package com.zd.service.impl;

import java.util.Date;
import java.util.List;

import com.zd.dao.ChapterDao;
import com.zd.dao.UserDao;
import com.zd.dao.impl.ChapterDaoImpl;
import com.zd.dao.impl.NovelDaoImpl;
import com.zd.dao.impl.UserDaoImpl;
import com.zd.entity.Chapter;
import com.zd.entity.Novel;
import com.zd.entity.PageBean;
import com.zd.entity.User;
import com.zd.service.AdminChapterService;

public class AdminChapterServiceImpl implements AdminChapterService {

	@Override
	public PageBean<Chapter> selectChapter(int aPageNum, int nowPage) {
		// TODO Auto-generated method stub
		ChapterDao ChapterDaoImpl = new ChapterDaoImpl();
		PageBean<Chapter> pageBean= new PageBean<Chapter>();
		
		int count=ChapterDaoImpl.getInforAllNum();
		int pageNumEnd=count%aPageNum==0?count/aPageNum:count/aPageNum+1;
		nowPage=nowPage<1?1:nowPage;
		nowPage=nowPage>pageNumEnd?pageNumEnd:nowPage;
		
		List<Chapter> userList= ChapterDaoImpl.selectChapterLimit((nowPage-1)*aPageNum,aPageNum);
//		List<User> userList=this.selectUserLimit(((nowPage-1)*aPageNum), aPageNum);
		pageBean.setaPageNum(aPageNum);
		pageBean.setCount(count);
		pageBean.setNowPage(nowPage);
		pageBean.setPageNumEnd(pageNumEnd);
		pageBean.setDate(userList);
		return pageBean;
	}

	@Override
	public int updateChapter(Chapter chapter) {
		// TODO Auto-generated method stub
		int num=ChapterDaoImpl.updateChpater(chapter);
		return num;
	}

	@Override
	public int deleteChapter(int chapterId) {
		// TODO Auto-generated method stub
		int num=ChapterDaoImpl.deleteChapter(chapterId);
		return num;
	}

	@Override
	public PageBean<Chapter> sousuoSelectUser(int aPageNum, int nowPage, String sousuo) {
		// TODO Auto-generated method stub
		PageBean<Chapter> pageBean= new PageBean<Chapter>();
		ChapterDao chapterdDoImpl = new ChapterDaoImpl();
		int count=chapterdDoImpl.getSousuoInforAllNum(sousuo);
		System.out.println(count+"这是数据条数");
		int pageNumEnd=count%aPageNum==0?count/aPageNum:count/aPageNum+1;
		nowPage=nowPage<1?1:nowPage;
		nowPage=nowPage>pageNumEnd?pageNumEnd:nowPage;
		
		List<Chapter> userList= chapterdDoImpl.sousuoSelectUserLimit((nowPage-1)*aPageNum,aPageNum,sousuo);
//		List<User> userList=this.selectUserLimit(((nowPage-1)*aPageNum), aPageNum);
		pageBean.setaPageNum(aPageNum);
		pageBean.setCount(count);
		pageBean.setNowPage(nowPage);
		pageBean.setPageNumEnd(pageNumEnd);
		pageBean.setDate(userList);
		System.out.println(aPageNum+"一页书");
		return pageBean;
	}

	@Override
	public int addChapter(String chapterName, int novelId, String text, Date date) {
		// TODO Auto-generated method stub
		int num=ChapterDaoImpl.addChapter(chapterName,novelId,text,date);
		return num;
	}

	
}
